﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {

        /// <summary>
        /// 显示设备上下文环境的句柄。
        /// </summary>
        private IntPtr _hdc = IntPtr.Zero;

        /// <summary>
        /// 指向窗口的句柄。
        /// </summary>
        private readonly IntPtr _hWnd = IntPtr.Zero;
        private void timer1_Tick(object sender, EventArgs e)
        {
            System.Drawing.Point p = MousePosition;

            uint color = Win32Helper.GetPixel(_hdc, p.X, p.Y);
            byte r = Win32Helper.GetRValue(color);
            byte g = Win32Helper.GetGValue(color);
            byte b = Win32Helper.GetBValue(color);


            this.pictureBox2.BackColor = Color.FromArgb(r, g, b);
            this.textBox2.Text = string.Format("{0},{1},{2}", Convert.ToString(r), Convert.ToString(g), Convert.ToString(b));
        }

        /// <summary>
        /// 取色按钮按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetColor_MouseDown(object sender, MouseEventArgs e)
        {
            _hdc = Win32Helper.GetDC(_hWnd);
            Cursor = Cursors.Cross;
            timer1.Enabled = true;
        }

        /// <summary>
        /// 取色按钮释放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetColor_MouseUp(object sender, MouseEventArgs e)
        {
            Win32Helper.ReleaseDC(_hWnd, _hdc);
            Cursor = Cursors.Default;
            timer1.Enabled = false;
        }

        public Form1()
        {
            InitializeComponent();
            try
            {
                string[] argbInt = this.textBox1.Text.Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                this.pictureBox1.BackColor = Color.FromArgb(int.Parse(argbInt[0]), int.Parse(argbInt[1]), int.Parse(argbInt[2]));
            }
            catch
            {
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string[] argbInt = this.textBox1.Text.Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                this.pictureBox1.BackColor = Color.FromArgb(int.Parse(argbInt[0]), int.Parse(argbInt[1]), int.Parse(argbInt[2]));
                Compare();
            }
            catch
            {
                this.textBox2.Text = "输入有误";
            }
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string[] argbInt = this.textBox2.Text.Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                this.pictureBox2.BackColor = Color.FromArgb(int.Parse(argbInt[0]), int.Parse(argbInt[1]), int.Parse(argbInt[2]));

                Compare();
            }
            catch
            {
                this.textBox2.Text = "输入有误";
            }
        }

        private void Compare()
        {
            try
            {
                string[] argbInt1 = this.textBox1.Text.Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string[] argbInt2 = this.textBox2.Text.Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var hsv1 = new HSV(int.Parse(argbInt1[0]), int.Parse(argbInt1[1]), int.Parse(argbInt1[2]));
                var hsv2 = new HSV(int.Parse(argbInt2[0]), int.Parse(argbInt2[1]), int.Parse(argbInt2[2]));
                this.label1.Text = "差异值：" + ColorUtil.DistanceOf(hsv1, hsv2);
            }
            catch
            {
                this.label1.Text = "输入有误";
            }
        }
    }
}
